﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace QuanLyNhaSach.Book
{
    public partial class FrmCategoryList : DevComponents.DotNetBar.Office2007Form
    {
        #region Private Variable

        BindingList<CategoryDTO> listCateBinding;
        int rowIndex; //Biến lưu giá trị của dòng đang sửa hơặc vừa thêm mới
        CategoryBUS categoryBUS;
        RuleObjectBUS RuleBUS;

        #endregion Private Variable

        #region Constructor

        public FrmCategoryList()
        {
            InitializeComponent();
            dtgCategoryList.AutoGenerateColumns = false;
            categoryBUS = new CategoryBUS();
            listCateBinding = new BindingList<CategoryDTO>();
            RuleBUS = new RuleObjectBUS();
            rowIndex = 0;
        }

        #endregion Constructor

        #region Private Methods

        private void CategoryList_Load(object sender, EventArgs e)
        {
            listCateBinding = categoryBUS.GetNewBindingListCategory();
            dtgCategoryList.DataSource = listCateBinding;
            this.VisibleChanged += new EventHandler(CategoryList_VisibleChanged);
            CheckPermission();
        }

        private void CheckPermission()
        {
            btnDel.Enabled = RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.ActionAuthor);
            btnUpdate.Enabled = btnDel.Enabled;
        }

        void CategoryList_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                Global.GenerateNumber(dtgCategoryList, colNumber.Index);
                Global.GenerateEditColumn(dtgCategoryList, colEdit.Index);
                Global.GenerateDeleteColumn(dtgCategoryList, colDel.Index);
            }
        }

        private void dtgCategoryList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //if (e.ColumnIndex == colEdit.Index)
            //{
            //    UpdateCategory(e.RowIndex);   
            //}
            //else if (e.ColumnIndex == colDel.Index)
            //{
            //    DeleteCategory();
            //}
        }

        #endregion Private Methods

        #region Public Methods

        public void CategoryChanged(CategoryDTO cateDTO)
        {
            CategoryDTO categoryDTO = categoryBUS.GetCategoryByID(cateDTO.CategoryID);

            dtgCategoryList[colCategoryName.Index, rowIndex].Value = categoryDTO.CategoryName;
        }

        public void CategoryChangeAfterInsert(CategoryDTO cateDTO)
        {
            CategoryDTO categoryDTO = categoryBUS.GetCategoryByID(cateDTO.CategoryID);          

            listCateBinding.Add(categoryDTO);

            rowIndex = dtgCategoryList.Rows.GetLastRow(DataGridViewElementStates.None);
            dtgCategoryList[colEdit.Index, rowIndex].Value = Properties.Resources.edit_16;
            dtgCategoryList[colDel.Index, rowIndex].Value = Properties.Resources.deletered;
            //Global.GenerateNumber(dtgCategoryList, colNumber.Index);
            if (rowIndex == 0)
            {
                dtgCategoryList[colNumber.Index, rowIndex].Value = rowIndex + 1;
            }
            else
            {
                dtgCategoryList[colNumber.Index, rowIndex].Value = dtgCategoryList[colNumber.Index, rowIndex - 1].Value.ToString();
            }
        }

        #endregion Public Methods

        #region Button,Delete, Update

        private void btnDel_Click(object sender, EventArgs e)
        {
            DeleteCategory();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            UpdateCategory(dtgCategoryList.CurrentRow.Index);
        }

        private void dtgCategoryList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (e.ColumnIndex == colEdit.Index)
                {
                    UpdateCategory(e.RowIndex);
                }
                else if (e.ColumnIndex == colDel.Index)
                {
                    DeleteCategory();
                }
            }
        }

        private void DeleteCategory()
        {
            if (dtgCategoryList.Rows.Count > 0)
            {
                if (!RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.ActionCategory))
                {
                    Global.SetMessage(lblMessage, "Bạn không có quyền thực hiện thao tác này!", false);
                    return;
                }

                if (Global.ShowMessageBoxDelete("Bạn chắc chắn muốn xóa thể loại này ?") == DialogResult.Yes)
                {
                    int success = 0, faile = 0;
                    foreach (DataGridViewRow row in dtgCategoryList.SelectedRows)
                    {
                        int cateID = int.Parse(row.Cells[colCategoryID.Index].Value.ToString());
                        if (categoryBUS.DeleteCategory(cateID))
                        {
                            listCateBinding.Remove(listCateBinding.First(c => c.CategoryID == cateID));
                            success++;
                        }
                        else
                        {
                            faile++;
                        }
                    }

                    Global.SetMessage(lblMessage, string.Format("Xóa thành công : {0} dòng, thất bại : {1} dòng.", success, faile), true);
                }
            }
        }

        private void UpdateCategory(int index)
        {
            if (!RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.ActionCategory))
            {
                Global.SetMessage(lblMessage, "Bạn không có quyền thực hiện thao tác này!", false);
                return;
            }

            rowIndex = index;
            CategoryDTO categoryDTO = new CategoryDTO
            {
                CategoryID = int.Parse(dtgCategoryList[colCategoryID.Index, rowIndex].Value.ToString()),
                CategoryCode = dtgCategoryList[colCategoryCode.Index, rowIndex].Value.ToString(),
                CategoryName = dtgCategoryList[colCategoryName.Index, rowIndex].Value.ToString()
            };


            Book.FrmCategoryDetail frm = new FrmCategoryDetail { Category = categoryDTO, Action = ActionName.Update };
            //frm.OnCategoryChanged += new Book.FrmCategoryDetail.CategoryHasChanged(CategoryChanged);
            frm.ShowDialog();
        }

        #endregion Button, Delete, Update
    }
}
